//
//  NSString+XJString.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/12.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "NSString+XJString.h"

@implementation NSString (XJString)

//验证手机号码是否合法
- (BOOL) isPhoneNumber{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(147)|(15[^4,\\D])|(17[0-9])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:self];
}

// 处理手机号
+ (NSString *)handleStringWithPhoneNumberStr:(NSString *)numberStr{
    NSString * handledStr = [numberStr stringByReplacingOccurrencesOfString:[numberStr substringWithRange:NSMakeRange(3, 4)] withString:@"****"];
    return handledStr;
}

//处理头像
+ (NSURL *)handleImgURLStringWithBaseImgURLString:(NSString *)baseURLStr
                                           ImgStr:(NSString *)imgStr{
    NSURL * imgUrl = [NSURL URLWithString:[[imgStr substringToIndex:4] isEqualToString:@"http"]?imgStr:[NSString stringWithFormat:@"%@%@",baseURLStr,imgStr]];;
    
    return imgUrl;
}

//处理个人头像
//+ (NSString *)handleImgURLStringWithImgStr:(NSString *)imgStr{
//    NSString * imgUrlStr = nil;
//    if ([imgStr rangeOfString:@"moren_0"].location != NSNotFound ) {
//        imgUrlStr = @"";
//    }else if (imgStr.length>4){
//        imgUrlStr = [[imgStr substringToIndex:4] isEqualToString:@"http"]?imgStr:[NSString stringWithFormat:@"%@%@",(NSString *)HANDLE_IMG_URL,imgStr];
//    }else{
//        imgUrlStr = @"";
//    }
//    return imgUrlStr;
//}

//判断字符串是否为空
+ (BOOL) isEmptyString:(NSString *)string{
    
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([string isEqualToString:@"(null)"]){
        return YES;
    }
    if ([string isEqualToString:@"<null>"]){
        return YES;
    }
    if ([string isEqualToString:@""]){
        return YES;
    }
    if ([string length]==0){
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0){
        return YES;
    }
    return NO;
}

/**
 6-16-1 去掉换换行符号，空格。。。。
 
 @param originStr 原始字符串
 @return 返回一个pureStr:无空格，换行符，。。。。
 */
+ (NSString *)pureStrWithOriginStr:(NSString *)originStr {
    // 去掉换行符号 与下面👇注释代码效果一样
    originStr = [originStr stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    
    originStr = [originStr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    originStr = [originStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    originStr = [originStr stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    originStr = [originStr stringByReplacingOccurrencesOfString:@" " withString:@""]; // 1-10这个只能取出普通空格。对于系统通讯录中的空格无能为力。
    
    originStr = [originStr stringByReplacingOccurrencesOfString:@"\0" withString:@""];
    originStr = [originStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
    originStr = [originStr stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    
    return originStr;
}


+ (BOOL) checkCardNo:(NSString*) cardNo{
    int oddsum = 0;     //奇数求和
    int evensum = 0;    //偶数求和
    int allsum = 0;
    int cardNoLength = (int)[cardNo length];
    int lastNum = [[cardNo substringFromIndex:cardNoLength-1]intValue];
    
    cardNo = [cardNo substringToIndex:cardNoLength -1];
    for (int i = cardNoLength -1; i>=1;i--) {
        NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1,1)];
        int tmpVal = [tmpString intValue];
        if (cardNoLength % 2 ==1) {
            if((i % 2) == 0){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }else{
            if((i % 2) == 1){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }
    }
    
    allsum = oddsum + evensum;
    allsum += lastNum;
    if((allsum % 10) == 0)
        return YES;
    else
        return NO;
}


//时间戳转时间
+ (NSString *)timeStrFromTimeStamp:(NSString *)timeStamp {
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    [fmt setDateStyle:NSDateFormatterMediumStyle];
    [fmt setTimeStyle:NSDateFormatterShortStyle];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue] / 1000.0];
    NSString *dateStr = [fmt stringFromDate:date];
    
    return dateStr;
}
@end
